package sz.szsmk.citizencard.ui.activity;

import android.text.TextUtils;
import android.view.View;

import com.chenenyu.router.annotation.Route;

import sz.szsmk.citizencard.DaggerApplication;
import sz.szsmk.citizencard.R;
import sz.szsmk.citizencard.databinding.ModifyPhoneActivityBinding;
import sz.szsmk.citizencard.dialog.SuccessDialog;
import sz.szsmk.citizencard.ioc.Compontent.DaggerCommonActivityComponent;
import sz.szsmk.citizencard.model.network.bean.response.BaseBean;
import sz.szsmk.citizencard.mvp.contract.IModifyPhoneContact;
import sz.szsmk.citizencard.mvp.presenter.ModifyPhonePresenter;
import sz.szsmk.citizencard.ui.base.IBaseActivity;
import sz.szsmk.citizencard.util.AndroidUtils;
import sz.szsmk.citizencard.util.PubUtils;
import sz.szsmk.citizencard.util.TimeCount;

/**
 * Copyright (c)2018
 * author: zdh
 * created on: 2018/7/10 13:58
 * description:
 */
@Route("modifyPhone")
public class ModifyPhoneActivity extends IBaseActivity<ModifyPhonePresenter, ModifyPhoneActivityBinding>
        implements IModifyPhoneContact.IModifyPhoneView, View.OnClickListener {

    private String userPhone;
    private String androidId;

    private TimeCount timeCount;

    @Override
    protected int attachLayoutRes() {
        return R.layout.modify_phone_activity;
    }

    @Override
    protected void initInjector() {
        DaggerCommonActivityComponent.builder().appCompontent(DaggerApplication.getAppComponent())
                .build().inject(this);
    }

    @Override
    protected void initViews() {
        dataBinding.titleLayout.settingTitle.setText("变更手机号");
        dataBinding.modifyOver.setOnClickListener(this);
        dataBinding.modifyGetcode.setOnClickListener(this);
        dataBinding.titleLayout.back.setOnClickListener(this);
        androidId = AndroidUtils.getAndroidId(DaggerApplication.getIns());
    }

    @Override
    protected void updateViews() {

    }

    @Override
    public void getCodeSuccess() {

    }

    @Override
    public void getCodeFalie(BaseBean response) {
        String message = response.getMessage();
        if (message == null) {
            return;
        }
        PubUtils.popTipOrWarn(this, message);
    }

    @Override
    public void checkCodeSucc(String message) {
        mPresenter.updateUserPhone(userPhone);
//        finish();
    }

    @Override
    public void checkCodeFail(String response) {
        PubUtils.popTipOrWarn(this, response);
    }

    @Override
    public void updateUserPhoneSucc(String message) {
        final SuccessDialog dialog = new SuccessDialog(this, "操作成功");
        dialog.show();
        new Thread() {
            @Override
            public void run() {
                super.run();
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                dialog.cancel();
            }
        }.start();
        finish();
    }

    @Override
    public void updateUserPhoneFail(String message) {
        PubUtils.popTipOrWarn(this, message);
    }

    private void setTimeCount() {
        timeCount = new TimeCount(dataBinding.modifyGetcode, 60000, 1000);
        timeCount.start();
        dataBinding.modifyGetcode.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        userPhone = dataBinding.modifyEt.getText().toString();
        int id = v.getId();
        if (id == R.id.modify_getcode) {
            if (TextUtils.isEmpty(userPhone)) {
                PubUtils.popTipOrWarn(this, "请输入您的手机号");
                return;
            }

            mPresenter.getCode(userPhone, androidId);

            setTimeCount();
//                timeCount.start();
        } else if (id == R.id.modify_over) {
            if (TextUtils.isEmpty(userPhone)) {
                PubUtils.popTipOrWarn(this, "请输入您的手机号");
                return;
            }
            String code = dataBinding.modifyInputcode.getText().toString();
            if (TextUtils.isEmpty(code)) {
                PubUtils.popTipOrWarn(this, "请输入验证码");
                return;
            }
            mPresenter.checkCode(code, userPhone, androidId);
        } else if (id == R.id.back) {
            finish();
        }
    }
}
